<%@ page contentType="text/html; charset=utf-8" %>
<%@page import="cn.bopomo.dealer.base.common.CommonConstant"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
.selectStyle{
	width:100px;
}
#op_div{
	text-align:center;
	font-size:12px;
}
.spanDiv{
margin:5px;
}
#datalist table{
border:1px solid #56626d;
font-size:12px;
}
#datalist th{
color:#FFFFFF;
background:#46525d;
text-align: center;
}
#datalist .even{
background-color:#eeeeee;
}
#datalist .even2{
background-color:#fff;
}
.tishi{
		float:right;
		width:100px;
		filter:Alpha(Opacity=100);
		opacity:100;
		position:absolute;
		background-color:#0099FF;
}
</style>
<script language="javascript" src="/js/json2.js"></script>
<script language="javascript" src="/js/litpen_ajax.js"></script>
<script language="javascript" type="text/javascript">
		//<![CDATA[
			var mid = <%=request.getParameter("mid")%>;
			
			/* -----发送AJAX请求_获取模块信息-----*/
			function loadModule(){
				var loadModule_ajax = new Ajax("/admin/loadModuleAjax.do?mid="+mid,null,true,loadModuleCallback,"get");
				loadModule_ajax.send();
			}
			
			/* -----接收AJAX返回信息_获取模块信息-----*/
			function loadModuleCallback(){
				//alert (arguments[1].length);
				var result = arguments[0];
				
				//alert(result);
				var myData = JSON.parse(result,null);
				//var moduleId = document.getElementById("moduleId");
				var moduleName = document.getElementById("moduleName");
				//var moduleAction = document.getElementById("moduleAction");
				//var moduleTag = document.getElementById("moduleTag");
				var addDate = document.getElementById("addDate");
				//moduleId.innerText = myData.moduleId;
				moduleName.innerText = myData.moduleName;
				//moduleAction.innerText = myData.moduleAction;
				//moduleTag.innerText = myData.moduleTag;
				addDate.innerText = myData.addDate;
			}
			
			
			function loadModules(){
				var module_ajax = new Ajax("/admin/showModulesAjax.do",null,true,loadModulesCallback,"get");
				module_ajax.send();
			}
			
			function loadAuthorizes(){
				var action = "/admin/showAuthorizeAjax.do" + "?mid=" + mid;
				var authorize_ajax = new Ajax(action,null,true,loadAuthorizeCallback,"get");
				authorize_ajax.send();
			}
			
			function loadGroups(){
				var action = "/admin/loadGroupsByMidAjax.do?mid=" + mid;
				var group_ajax = new Ajax(action,null,true,loadGroupsCallback,"get");
				group_ajax.send();
			}
			
			function loadGroupsCallback(){
				var result = arguments[0];
				var myData = JSON.parse(result,null);
				var oTable = document.createElement("TABLE");
				
				oTable.setAttribute("width","95%");
				oTable.setAttribute("align","center");
				oTable.setAttribute("cellpadding","2");
				oTable.setAttribute("cellspacing","1");
				oTable.setAttribute("id","dataTable");
				
				var oTHead = document.createElement("THEAD");
				var oTBody = document.createElement("tbody");
				oTable.appendChild(oTHead);
				oTable.appendChild(oTBody);
				var heading = new Array;
				heading[0] = "权限组ID";
				heading[1] = "权限组名称";
				heading[2] = "TEMP";
				heading[3] = "相关操作";
				oRow = document.createElement("TR");
				oTHead.appendChild(oRow);
				for (i=0; i<heading.length; i++){
  					oCell = document.createElement("TH");
  					oCell.innerText = heading[i];
  					oRow.appendChild(oCell);
				} 
				
				for (var i = 0; i < myData.groups.length; i++){
					oRow = document.createElement("TR");
					
					if (i % 2 == 0)
						oRow.className = "even";
					else
						oRow.className = "even2";
					oTBody.appendChild(oRow);
					oCellId = document.createElement("TD");
					oCellId.innerText = myData.groups[i].groupId;
					oRow.appendChild(oCellId);
					
					oCellName = document.createElement("TD");
					oCellName.innerText = myData.groups[i].groupName;
					oRow.appendChild(oCellName);
					
					oCellTmp = document.createElement("TD");
					oCellTmp.innerText = myData.groups[i].groupName;
					oRow.appendChild(oCellTmp);
					
					oCellOp = document.createElement("TD");
					oCellOp.innerHTML = '<input type="button" value="编辑" ref="006" onclick="javascript:pre_editGroup(' + myData.groups[i].groupId + ');"/>&nbsp;&nbsp;&nbsp;<input type="button" value="删除" ref="007" onclick="javascript:deleteGroup(' + myData.groups[i].groupId + ');"/>';
					oRow.appendChild(oCellOp);
				}
				
				var datalist = document.getElementById("datalist");
				datalist.innerHTML = "";
				datalist.appendChild(oTable);
				initDiv(user_moduleList);
			}
			
			function loadModulesCallback(){
				var result = arguments[0];
				var myData = JSON.parse(result,null);
				var module_select = document.getElementById("moduleId");
				if (module_select){
					module_select.innerHTML = "";
					for (var i = 0;i<myData.modules.length;i++){
						var option = document.createElement("option");
						option.setAttribute("value",myData.modules[i].mid);
						
						option.text= myData.modules[i].moduleName;
						module_select.add(option);
					}
					loadAuthorizes(module_select);
				}
			}
			
			function loadAuthorizeCallback(){
				var result = arguments[0];
				if (result =="")
					return;
				var myData = JSON.parse(result,null);
				
				var authorize_list = document.getElementById("from");
				
				if (authorize_list){
					
					authorize_list.innerHTML = "";
					for (var i = 0;i<myData.authorizes.length;i++){
						var option = document.createElement("option");
						option.setAttribute("value",myData.authorizes[i].tag);
						option.text= myData.authorizes[i].authorizeName;
						
						authorize_list.add(option);
					}
				}
				initDiv(user_moduleList);
			}
			
			
			function copyToList(from,to){
				fromList = eval('document.forms[0].' + from);
				toList = eval('document.forms[0].' + to);
				if (toList.options.length > 0 && toList.options[0].value == 'temp'){
					toList.options.length = 0;
				}
				var sel = false;
				for (i=0;i<fromList.options.length;i++){
					var current = fromList.options[i];
					if (current.selected){
						sel = true;
						if (current.value == 'temp'){
							alert ('你不能选择这个项目!');
							return;
						}
						txt = current.text;
						val = current.value;
						toList.options[toList.length] = new Option(txt,val);
						
						fromList.options[i] = null;
						i--;
					}
				}
				if (!sel) alert ('你还没有选择任何项目');
			}
			function copyAll(from,to){
				fromList = eval('document.forms[0].' + from);
				toList = eval('document.forms[0].' + to);
				if (toList.options.length > 0 && toList.options[0].value == 'temp'){
					toList.options.length = 0;
				}
				for (i=0;i<fromList.options.length;i++){
					var current = fromList.options[i];
					if (current.value == 'temp'){
							alert ('你不能选择这个项目!');
							return;
					}
					txt = current.text;
					val = current.value;
					toList.options[toList.length] = new Option(txt,val);
					fromList.options[i] = null;
					i--;
				}
			}
			
			function groupAuthorizeInit(form){
				var str = "";
				for (var i=0;i<form.to.length;i++){
					str += form.to[i].value + "/";
				}
				form.groupAuthorize.value= str;
				
			}
			
			function submitAdd(){
				var form = document.forms['GroupForm'];
				groupAuthorizeInit(form);
				var submitType = new Array();
				
				//校验form是否合法
				if  (!checkGroupForm(form)){
					return;
				}
				
				/*--- 判断操作类型 进行form action的修改 ---*/
				if (form.btnAction.innerText == '修改'){
					form.action = "/admin/groupModify.do";
					submitType.push("modify");
				}else{
					submitType.push("add");
				}
				var addGroup_ajax = new Ajax(form.action,formToRequestString(form),true,addGroupCallback,"post",submitType);
				addGroup_ajax.send();
			}
			
			function addGroupCallback(){
				var result = arguments[0];
				var msgs = result.split("||");
				
				var resultDiv = newResultDiv(msgs[1]);
				var body = document.body;
				//alert(resultDiv.outerHTML);
				//alert(document.body.offsetHeight);
				body.innerHTML += resultDiv.outerHTML;
				
				//var resultFilter = new ResultFilter();
				
				//while (resultFilter.alpha >0){
				//	window.setTimeout(_change,500,resultDiv.id,resultFilter);
					//resultFilter.alpha --;
				//}
				//resultDiv.style.display = "none";
				
				//alert(resultDiv);
				var type = arguments[1];
				if (type != null){
					if (type[0] == "add")
						loadGroups();
				}
				clearGroupForm();
				initDiv(user_moduleList);
			}
			
			function newResultDiv(msg){
				var resultDiv = document.createElement("div");
				resultDiv.setAttribute("class","tishi");
				resultDiv.innerHTML=msg;
				pageX=window.document.body.scrollLeft;
				pageW=window.document.body.offsetWidth-8;
				pageY=window.document.body.scrollTop;
				pageH=window.document.body.offsetHeight;
				//resultDiv.style.left = document.body.offsetHeight;
				resultDiv.style.left = pageW-150;
				resultDiv.style.top = pageY;
				resultDiv.id = "test";
				return resultDiv;
			}
			function _change(_resultDiv,resultFilter){
				var div = eval("document.getElementById('" + _resultDiv + "')");
				change(div,resultFilter);
				
			}
			function change(resultDiv,resultFilter){
				//alert(resultFilter.getAlpha());
				resultDiv.style.filter = "Alpha(Opacity=" + resultFilter.getAlpha() + ")"; //for IE    
        		resultDiv.style.opacity = resultFilter.getAlpha()/100; //for FF
				resultFilter.setAlpha(resultFilter.getAlpha() - 1);
				if (resultFilter.getAlpha() == 0){
					resultDiv.style.display = "none";
				}
				
			}
			function ResultFilter(){
				this.alpha = 100;
				if (typeof ResultFilter._initialized == "undefined") {
    
       				ResultFilter.prototype.getAlpha = function () {
            			return this.alpha;
        			}; 
					
					ResultFilter.prototype.setAlpha = function (alpha) {
            			this.alpha = alpha;
        			};   
        
        		ResultFilter._initialized = true;
    			}
			}
			
			var _st = window.setTimeout;
			window.setTimeout = function(fRef, mDelay) { 
				if(typeof fRef == 'function'){  
					var argu = Array.prototype.slice.call(arguments,2);  
					var f = (function(){ fRef.apply(null, argu); });
					return _st(f, mDelay); 
				} 
				return _st(fRef,mDelay);
			}
			
			function pre_editGroup(id){
			
				var loadGroupAction = "/admin/loadGroupByGidAjax.do" + "?gid=" + id;
				var container = new Array();
				//alert(container.length);
				var loadGroup_ajax = new Ajax(loadGroupAction,null,true,loadGroupCB,"get",container);
				loadGroup_ajax.send();
				//alert(container.length);
				//var action = "/admin/showAuthorizeAjax.do" + "?mid=" + mid;
				//var pre_editGroup_ajax = new Ajax(action,null,true,pre_editGroupCB,"get");
				//pre_editGroup.send();
			}
			
			
			
			function loadGroupCB(res,cbContainer){
				//alert("here");
				var result = arguments[0];
				var group = JSON.parse(result,null);
				var form = document.forms['GroupForm'];
				form.groupAuthorize.value = group.groupAuthorize;
				form.groupName.value = group.groupName;
				var toList = form.to;
				var fromList = form.from;
				toList.innerHTML = "";
				fromList.innerHTML = "";
				var id_input = document.createElement("input");
				id_input.setAttribute("type","hidden");
				id_input.setAttribute("name","gid");
				id_input.setAttribute("value",group.id);
				form.appendChild(id_input);
				for (var i = 0; i<group.toArray.length;i++){
					var txt = group.toArray[i].authorizeName;
					var val = group.toArray[i].tag;
					toList.options[i] = new Option(txt,val);
				}
				for (var j = 0; j<group.fromArray.length;j++){
					var txt = group.fromArray[j].authorizeName;
					var val = group.fromArray[j].tag;
					fromList.options[j] = new Option(txt,val);
				}
				form.btnAction.innerHTML = "修改";	
			}
			
			function deleteGroup(id){
				if (!confirm("确定要删除吗？"))
					return;
				var deleteAction = "/admin/deleteGroup.do?gid=" + id;
				var deleteGroup_ajax = new Ajax(deleteAction,null,true,deleteGroupCB,"get");
				deleteGroup_ajax.send();
			}
			
			function deleteGroupCB(){
				var result = arguments[0];
				var msgs = result.split("||");
				if (msgs[0] == "00"){
					loadGroups();
					alert(msgs[1]);
				}
				else
					alert(msgs[1]);
			}
			
			function checkGroupForm(groupForm){
				var returnVal = false;
				var msg = "";
				if (groupForm.groupName.value == "")
					msg += "权限组名称不能为空\n";
				
				if (groupForm.groupAuthorize.value == "")
					msg += "请选择权限组应有的权限\n";
				
				if (msg == "")
					returnVal = true;
				else
					alert(msg);
					
				return returnVal;
			}

			
			function clearGroupForm(){
				var form = document.forms['GroupForm'];
				form.action = "/admin/addGroup.do";
				form.groupAuthorize.value = "";
				form.groupName.value = "";
				form.to.innerHTML = "";
				form.btnAction.innerHTML = "添加";
				var id_input = form.gid;
				if (typeof id_input == "Object")
					form.removeChild(id_input);
				loadAuthorizes();	
			}
		//]]>
</script>
</head>

<body>
	<div id="moduleDetail">
		<TABLE width="95%" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" style="border:1px solid #56626d;font-size:12px">
		  <TR bgcolor="#FFFFFF">
		    <TD colspan="3" align="left" style="font-size:14px"><B>&nbsp;&nbsp;权限管理</B></TD>
		  </TR>
		  <TR bgcolor="#FFFFFF">
		    <TD style="background:#46525d;text-align: center;">
		    	<span style="background:#46525d;color:#FFFFFF;text-align: center;">
					<div id="moduleName"></div>
				</span>
			</TD>
		  </TR>
		  <TR bgcolor="#EEEEEE">
		    <TD nowrap align="center">
				创建日期: <span id="addDate">正在加载数据......</span>
			</TD>
		  </TR>
		</TABLE>
	</div>
	<div class="spanDiv"></div>
	<div id="op_div" ref="005">
		<html:form method="POST" action="/admin/addGroup" >
			<html:hidden name="GroupForm" property="moduleId" styleId="moduleId" value="<%=request.getParameter("mid")%>"/>
			<html:hidden name="GroupForm" property="groupAuthorize"/>
			权限组名称：<html:text name="GroupForm" property="groupName"/><br/>
			<table>
				<tr>
				<td>
			<select size="10"  multiple name="from" class="selectStyle" id="from"  >
			
		  </select>
		</td>
		<td><button onClick="copyAll('from','to');">->></button><br/>
		<button onClick="copyToList('from','to');">--></button><br/>
		<button onClick="copyToList('to','from');"><--</button><br/>
		<button onClick="copyAll('to','from');"><<-</button>
		</td>
		<td>
		<select size="10"  multiple name="to" class="selectStyle">
		</select>
		</td>
		</tr>
		</table>
		<button onClick="submitAdd(this);" name="btnAction">添加</button>&nbsp;&nbsp;&nbsp;<button onClick="clearGroupForm();" name="btnCancel">取消</button>
		</html:form>
	</div>
	<div class="spanDiv"></div>
	<div id="datalist">
		加载中.....
	</div>
	<script language="javascript">
		loadGroups();
		loadAuthorizes();
		loadModule();
	</script>
</body>
</html>
